﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DotNetX.UI.Web
{
    /// <summary>
    /// This is a standard label but is translatable
    /// </summary>
    [Serializable()]
    public class TranslatableLabel : System.Web.UI.WebControls.Label, ITranslatableControl
    {
        #region ITranslatableControl Members

        /// <summary>
        /// The translation key for this control
        /// </summary>
        public string TranslationKey
        {
            get
            {
                // return the value
                return this._translationKey;
            }
            set
            {
                // make sure we have a value
                if (string.IsNullOrEmpty(value))
                    throw new ArgumentException("A translation key must be provided.");

                // set the value
                this._translationKey = value;
            }
        }
        private string _translationKey = string.Empty;

        /// <summary>
        /// Shall we translate this control
        /// </summary>
        public bool ShallTranslate
        {
            get
            {
                // return the value
                return this._shallTranslate;
            }
            set
            {
                // set the value
                this._shallTranslate = value;
            }
        }
        private bool _shallTranslate = true;

        /// <summary>
        /// The current language of this control
        /// </summary>
        public Language CurrentLanguage
        {
            get
            {
                // return the current language
                return this._currentLanguage;
            }
        }
        private Language _currentLanguage = Languages.DefaultLanguage;

        /// <summary>
        /// This translates this text into that of the target language
        /// </summary>
        /// <param name="targetLanguage">The language to translate into</param>
        /// <returns>True if the translation was a success, else false</returns>
        public bool Translate(Language targetLanguage)
        {
            if (targetLanguage == null)
                throw new ArgumentNullException("'targetLanguage' is null.");

            // only translate if we havn't been translated yet or if we are on a new language
            if (this.Text.Length > 0 &&
                this._currentLanguage == targetLanguage)
                return true;

            // get the translated text
            string translation = Languages.GetTranslationByKey(targetLanguage, this.TranslationKey);

            // success?
            if(string.IsNullOrEmpty(translation))
            {
                // no
                return false;
            }

            // set the translation
            this.Text = translation;

            // set the new language
            this._currentLanguage = targetLanguage;

            // success
            return true;
        }

        #endregion
    }
}
